$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'jobinfo/pageList',
        mtype: "POST",
        datatype: "json",
        colModel: [
			{ label: '任务ID', name: 'id', index: 'id', width: 50, key: true, hidden: true},
			{ label: '任务名称', name: 'name', index: 'name', width: 50},
			{ label: '任务描述', name: 'jobDesc', index: 'jobDesc', width: 80 },
            { label: '调度类型', name: 'scheduleType', sortable: false, width: 75, formatter: function(value, options, row){
                    if (row.scheduleConf) {
                        return row.scheduleType + '：'+ row.scheduleConf;
                    } else {
                        return row.scheduleType;
                    }
            }},
            { label: '运行模式', name: 'executorHandler', sortable: false, width: 75, formatter: function(value, options, row){
                    var glueTypeTitle = row.glueType;
                    if (row.executorHandler) {
                        return glueTypeTitle +"：" + row.executorHandler;
                    } else {
                        return glueTypeTitle;
                    }
            }},
			{ label: '负责人', name: 'author', sortable: false, width: 80},
			{ label: '状态 ', name: 'triggerStatus', sortable: false, width: 50,search: false, formatter: function(value, options, row){
                    // status
                    if (1 == value) {
                        return '<small class="label label-success" >RUNNING</small>';
                    } else {
                        return '<small class="label label-default" >STOP</small>';
                    }
			}},
			{ label: '操作', name: 'id', index: 'id', width: 80,search:false, formatter: function(value, options, row){

                    var html = '<a href="javascript:void(0);" onclick="vm.triggerJob(\''+value+'\')" class="btn btn-primary btn-label">&nbsp;执行一次</a>';
                    if(row.triggerStatus == 1){
                        html += '&nbsp;<a href="javascript:void(0);" onclick="vm.stopJob(\''+value+'\')" class="btn btn-danger btn-label">&nbsp;停止</a>';
                    }else{
                        html += '&nbsp;<a href="javascript:void(0);" onclick="vm.startJob(\''+value+'\')" class="btn btn-primary btn-label">&nbsp;启动</a>';
                    }

                    return html;
            }}
        ],
		viewrecords: true,
        height: 'auto',
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        //width: document.body.clientWidth,根据body的宽度使用这个请注释掉autowidth:true
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "data.data",
            page: "data.pageNum",
            total: "data.pages",
            records: "data.recordCounts"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
    $("#jqGrid").filterToolbar({}).jqGrid('navGrid',"#jqGridPager",{//刷新按钮
        refresh: true,
        edit:false,del: false,search:false,add:false
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        showJobInfoAdd: false,
        triggerStatusEnable: false,
        autoRegister: true,
		title: null,
        jobinfo: {
            triggerStatus: 0,
            addressList: null,
            addressType: 0,
            executorHandler: 'executorJobHandler',
            scheduleConf: '0 0 1 * * ?'
        },
        q:{
            jobGroup: -1,
            triggerStatus: -1
        }
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showJobInfoAdd = true;
			vm.title = "新增";
            $("#jobInfoForm")[0].reset();
            $("#scheduleType").val("CRON");
            vm.jobinfo = {triggerStatus: 0,addressType: 0, executorHandler: 'executorJobHandler',scheduleConf: '0 0 1 * * ?'};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showJobInfoAdd = true;
            vm.title = "修改";

            vm.getInfo(id)
		},
        triggerJob: function(jobId){
            layer.confirm('确定要执行一次吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: "GET",
                    url: baseURL + "jobinfo/trigger?id="+jobId,
                    contentType: "application/json",
                    success: function(r){
                        if(r.code === 200){
                            layer.msg("操作成功", {icon: 1});
                        }else{
                            layer.alert(r.msg);
                        }
                    }
                });
            }, function () {
            });
        },
        startJob: function(jobId){
            $.ajax({
                type: "GET",
                url: baseURL + "jobinfo/start?id="+jobId,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 200){
                        layer.msg("操作成功", {icon: 1});
                        vm.reload();
                    }else{
                        layer.alert(r.msg);
                    }
                }
            });
        },
        stopJob: function(jobId){
            $.ajax({
                type: "GET",
                url: baseURL + "jobinfo/stop?id="+jobId,
                contentType: "application/json",
                success: function(r){
                    if(r.code === 200){
                        layer.msg("操作成功", {icon: 1});
                        vm.reload();
                    }else{
                        layer.alert(r.msg);
                    }
                }
            });
        },
        changeAddressType: function(event){
            var val=$('input:radio[name="addressType"]:checked').val();
            if(val == 1){
                vm.autoRegister = false;
            }else{
                vm.autoRegister = true;
            }
        },
        convertSelectVal: function(obj){
            if(!obj){
                // obj不存在则获取值绑定到vm对象
                vm.jobinfo.jobGroup = $("#jobGroup").val();
                vm.jobinfo.executorRouteStrategy = $("#executorRouteStrategy").val();
                vm.jobinfo.glueType = $("#glueType").val();
                vm.jobinfo.executorBlockStrategy = $("#executorBlockStrategy").val();
                vm.jobinfo.misfireStrategy = $("#misfireStrategy").val();
                vm.jobinfo.scheduleType = $("#scheduleType").val();
            }else{
                // obj存在则获取obj值绑定到dom对象
                $("#jobGroup").val(vm.jobinfo.jobGroup);
                $("#executorRouteStrategy").val(vm.jobinfo.executorRouteStrategy);
                $("#glueType").val(vm.jobinfo.glueType);
                $("#executorBlockStrategy").val(vm.jobinfo.executorBlockStrategy);
                $("#misfireStrategy").val(vm.jobinfo.misfireStrategy);
                $("#scheduleType").val(vm.jobinfo.scheduleType);
            }
        },
        saveOrUpdateJobInfo: function(event){
		    vm.saveOrUpdate(event);
        },
		saveOrUpdate: function (event) {
            var url = vm.jobinfo.id == null ? "jobinfo/add" : "jobinfo/update";
            vm.convertSelectVal();
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.jobinfo),
                success: function(r){
                    if(r.code === 200){
                        layer.msg("操作成功", {icon: 1});
                        vm.reload();
                    }else{
                        layer.alert(r.msg);
                    }
                }
            });
		},
		del: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "jobinfo/remove?id="+id,
                        success: function(r){
                            if(r.code == 200){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "jobinfo/loadById?id="+id, function(r){
                if(r.code == 200){
                    vm.jobinfo = r.content;
                    vm.convertSelectVal(vm.jobinfo);
                }else{
                    alert(r.msg);
                }
            });
		},
        jobInfoAddBack: function(){
		    vm.reload();
        },
		reload: function (event) {
			vm.showJobInfoAdd = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'jobGroup': vm.q.jobGroup,
                    'triggerStatus': vm.q.triggerStatus
                },
                page:page
            }).trigger("reloadGrid");
		},
        showSenior: function(obj, domId){
            var senior = obj.innerHTML;
            if(senior == "展开"){
                $("#"+domId).show();
                event.target.innerHTML = "隐藏";
            }else{
                $("#"+domId).hide();
                event.target.innerHTML = "展开";
            }
        }
	}
});